package cc.eddic.practice.concurrency.impl;

import cc.eddic.practice.concurrency.LogItem;
import cc.eddic.practice.concurrency.LogItemParser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Slf4j
@Component
@Primary
public class LogItemParserDefaultImpl implements LogItemParser {

    @Override
    public Optional<LogItem> parse(String line) {
        Optional<LogItem> result = nginx.parse(line);
        if (!result.isPresent()) {
            result = qiniuCdn.parse(line);
        }
        return result;
    }

    public LogItemParserDefaultImpl(@Qualifier("LogItemParserImpl") LogItemParser nginx, @Qualifier("LogItemParserQiniuCdnImpl") LogItemParser qiniuCdn) {
        this.nginx = nginx;
        this.qiniuCdn = qiniuCdn;
    }

    private final LogItemParser nginx;
    private final LogItemParser qiniuCdn;
}
